<html>
<head>
    <script src="../../OLLoader.js"></script>
    <script type="text/javascript">

        function test_ows(t) {
            t.plan(20);
            var xml = document.getElementById("ogcsample").firstChild.nodeValue;
            var doc = new OpenLayers.Format.XML().read(xml);
            var obj = new OpenLayers.Format.WMTSCapabilities().read(doc);
            // ows:ServiceIdentification
            var serviceIdentification = obj.serviceIdentification;
            t.eq(serviceIdentification.title, "Web Map Tile Service", "ows:ServiceIdentification title is correct");
            t.eq(serviceIdentification.serviceTypeVersion, "1.0.0", "ows:ServiceIdentification serviceTypeVersion is correct");
            t.eq(serviceIdentification.serviceType.value, "OGC WMTS", "ows:ServiceIdentification serviceType is correct");

            // ows:ServiceProvider
            var serviceProvider = obj.serviceProvider;
            t.eq(serviceProvider.providerName, "MiraMon", "ows:ServiceProvider providerName is correct");
            t.eq(serviceProvider.providerSite, "http://www.creaf.uab.es/miramon", "ows:ServiceProvider providerSite is correct");
            t.eq(serviceProvider.serviceContact.individualName, "Joan Maso Pau", "ows:ServiceProvider individualName is correct");
            t.eq(serviceProvider.serviceContact.positionName, "Senior Software Engineer", "ows:ServiceProvider positionName is correct");
            t.eq(serviceProvider.serviceContact.contactInfo.address.administrativeArea, "Barcelona", "ows:ServiceProvider address administrativeArea is correct");
            t.eq(serviceProvider.serviceContact.contactInfo.address.city, "Bellaterra", "ows:ServiceProvider address city is correct");
            t.eq(serviceProvider.serviceContact.contactInfo.address.country, "Spain", "ows:ServiceProvider address country is correct");
            t.eq(serviceProvider.serviceContact.contactInfo.address.deliveryPoint, "Fac Ciencies UAB", "ows:ServiceProvider address deliveryPoint is correct");
            t.eq(serviceProvider.serviceContact.contactInfo.address.electronicMailAddress, "joan.maso@uab.es", "ows:ServiceProvider address electronicMailAddress is correct");
            t.eq(serviceProvider.serviceContact.contactInfo.address.postalCode, "08193", "ows:ServiceProvider address postalCode is correct");
            t.eq(serviceProvider.serviceContact.contactInfo.phone.voice, "+34 93 581 1312", "ows:ServiceProvider phone voice is correct");

            // ows:OperationsMetadata
            var operationsMetadata = obj.operationsMetadata;
            t.eq(operationsMetadata.GetCapabilities.dcp.http.get[0].url, "http://www.miramon.uab.es/cgi-bin/MiraMon5_0.cgi?", "ows:OperationsMetadata GetCapabilities url is correct");
            t.eq(operationsMetadata.GetCapabilities.dcp.http.get[0].constraints.GetEncoding.allowedValues,
                 {'KVP': true},
                 "ows:OperationsMetadata GetCapabilities Constraints Get is correct");
            t.eq(operationsMetadata.GetFeatureInfo.dcp.http.get[0].url, "http://www.miramon.uab.es/cgi-bin/MiraMon5_0.cgi?", "ows:OperationsMetadata GetFeatureInfo url is correct");
            t.eq(operationsMetadata.GetFeatureInfo.dcp.http.get[0].constraints,
                 undefined,
                 "ows:OperationsMetadata GetFeatureInfo Constraints Get is correct");
            t.eq(operationsMetadata.GetTile.dcp.http.get[0].url, "http://www.miramon.uab.es/cgi-bin/MiraMon5_0.cgi?", "ows:OperationsMetadata GetTile url is correct");
            t.eq(operationsMetadata.GetTile.dcp.http.get[0].constraints,
                 undefined,
                 "ows:OperationsMetadata GetTile Constraints Get is correct");
        }
    
        function test_layers(t) {
            t.plan(37);
            var xml = document.getElementById("ogcsample").firstChild.nodeValue;
            var doc = new OpenLayers.Format.XML().read(xml);
    
            var obj = new OpenLayers.Format.WMTSCapabilities().read(doc);
            var contents = obj.contents;
    
            var numOfLayers = contents.layers.length;
            t.eq(numOfLayers, 1, "correct count of layers");

            var layer = contents.layers[0];
            t.eq(layer['abstract'], "Coastline/shorelines (BA010)", "layer abstract is correct");
            t.eq(layer.identifier, "coastlines", "layer identifier is correct");
            t.eq(layer.title, "Coastlines", "layer title is correct");

            var numOfFormats = layer.formats.length;
            t.eq(numOfFormats, 2, "correct count of formats");
            t.eq(layer.formats[0], "image/png", "format image/png is correct");
            t.eq(layer.formats[1], "image/gif", "format image/gif is correct");

            var numOfStyles = layer.styles.length;
            t.eq(numOfStyles, 2, "correct count of styles");
            t.eq(layer.styles[0].identifier, "DarkBlue", "style 0 identifier is correct");
            t.eq(layer.styles[0].isDefault, true, "style 0 isDefault is correct");
            t.eq(layer.styles[0].title, "Dark Blue", "style 0 title is correct");
            t.eq(layer.styles[0].legend.href, "http://www.miramon.uab.es/wmts/Coastlines/coastlines_darkBlue.png", "style 0 legend href is correct");
            t.eq(layer.styles[0].legend.format, "image/png", "style 0 legend format is correct");
            t.eq(layer.styles[1].identifier, "thickAndRed", "style 1 identifier is correct");
            t.ok(!layer.styles[1].isDefault, "style 1 isDefault is correct");
            t.eq(layer.styles[1].title, "Thick And Red", "style 1 title is correct");
            t.eq(layer.styles[1].legend, undefined, "style 1 legend is not set");
            //t.eq(layer.styles[1].abstract, "Specify this style if you want your maps to have thick red coastlines. ", "style 1 abstract is correct");

            t.eq(layer.tileMatrixSetLinks.length, 1, "correct count of tileMatrixSetLinks");
            t.eq(layer.tileMatrixSetLinks[0].tileMatrixSet, "BigWorld", "tileMatrixSet is correct");

            var wgs84Bbox = layer.bounds;
            t.ok(wgs84Bbox instanceof OpenLayers.Bounds, "wgs84BoudingBox instance of OpenLayers.Bounds");
            t.eq(wgs84Bbox.left, -180.0, "wgs84BoudingBox left is correct");
            t.eq(wgs84Bbox.right, 180.0, "wgs84BoudingBox right is correct");
            t.eq(wgs84Bbox.bottom, -90.0, "wgs84BoudingBox bottom is correct");
            t.eq(wgs84Bbox.top, 90.0, "wgs84BoudingBox top is correct");

            t.eq(layer.resourceUrl.tile.format, "image/png", "resourceUrl.tile.format is correct");
            t.eq(layer.resourceUrl.tile.template, "http://www.example.com/wmts/coastlines/{TileMatrix}/{TileRow}/{TileCol}.png", 
                 "resourceUrl.tile.template is correct");

            t.eq(layer.resourceUrl.FeatureInfo.format, "application/gml+xml; version=3.1", "resourceUrl.FeatureInfo.format is correct");
            t.eq(layer.resourceUrl.FeatureInfo.template, "http://www.example.com/wmts/coastlines/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}/{J}/{I}.xml", 
                 "resourceUrl.FeatureInfo.template is correct");

            var dimensions = layer.dimensions;
            t.eq(dimensions.length, 1, "correct count of dimensions");
            t.eq(dimensions[0].title, "Time", "first dimension title is correct");
            t.eq(dimensions[0]['abstract'], "Monthly datasets", "first dimension abstract is correct");
            t.eq(dimensions[0].identifier, "TIME", "first dimension identifier is correct");
            t.eq(dimensions[0]['default'], "default", "first dimension default is correct");
            t.eq(dimensions[0].values.length, 3, "first dimension has correct count of values");
            t.eq(dimensions[0].values[0], "2007-05", "first value is correct");
            t.eq(dimensions[0].values[1], "2007-06", "second value is correct");
            t.eq(dimensions[0].values[2], "2007-07", "third value is correct");
        }

        function test_tileMatrixSets(t) {
            t.plan(19);
            var xml = document.getElementById("ogcsample").firstChild.nodeValue;
            var doc = new OpenLayers.Format.XML().read(xml);
    
            var obj = new OpenLayers.Format.WMTSCapabilities().read(doc);

            var tileMatrixSets = obj.contents.tileMatrixSets;
            t.ok(tileMatrixSets['BigWorld'], "tileMatrixSets 'BigWorld' found");
            var bigWorld = tileMatrixSets['BigWorld'];
            t.eq(bigWorld.identifier, "BigWorld", "tileMatrixSets identifier is correct");
            t.eq(bigWorld.matrixIds.length, 2, "tileMatrix count is correct");
            t.eq(bigWorld.matrixIds[0].identifier, "1e6", "tileMatrix 0 identifier is correct");
            t.eq(bigWorld.matrixIds[0].matrixHeight, 50000, "tileMatrix 0 matrixHeight is correct");
            t.eq(bigWorld.matrixIds[0].matrixWidth, 60000, "tileMatrix 0 matrixWidth is correct");
            t.eq(bigWorld.matrixIds[0].scaleDenominator, 1000000, "tileMatrix 0 scaleDenominator is correct");
            t.eq(bigWorld.matrixIds[0].tileWidth, 256, "tileMatrix 0 tileWidth is correct");
            t.eq(bigWorld.matrixIds[0].tileHeight, 256, "tileMatrix 0 tileHeight is correct");
            t.eq(bigWorld.matrixIds[0].topLeftCorner.lon, -180, "tileMatrix 0 topLeftCorner.lon is correct");
            t.eq(bigWorld.matrixIds[0].topLeftCorner.lat, 84, "tileMatrix 0 topLeftCorner.lat is correct");

            t.eq(bigWorld.matrixIds[1].identifier, "2.5e6", "tileMatrix 1 identifier is correct");
            t.eq(bigWorld.matrixIds[1].matrixHeight, 7000, "tileMatrix 1 matrixHeight is correct");
            t.eq(bigWorld.matrixIds[1].matrixWidth, 9000, "tileMatrix 1 matrixWidth is correct");
            t.eq(bigWorld.matrixIds[1].scaleDenominator, 2500000, "tileMatrix 1 scaleDenominator is correct");
            t.eq(bigWorld.matrixIds[1].tileWidth, 256, "tileMatrix 1 tileWidth is correct");
            t.eq(bigWorld.matrixIds[1].tileHeight, 256, "tileMatrix 1 tileHeight is correct");
            t.eq(bigWorld.matrixIds[1].topLeftCorner.lon, -180, "tileMatrix 1 topLeftCorner.lon is correct");
            t.eq(bigWorld.matrixIds[1].topLeftCorner.lat, 84, "tileMatrix 1 topLeftCorner.lat is correct");
        }
        
        function test_createLayer(t) {
            t.plan(7);
            
            var format = new OpenLayers.Format.WMTSCapabilities();

            var xml = document.getElementById("ogcsample").firstChild.nodeValue;
            var doc = new OpenLayers.Format.XML().read(xml);
    
            var caps = format.read(doc);
            var layer;
            
            var success = true;
            try {
                // incomplete config (missing matrixSet)
                layer = format.createLayer(caps, {
                    layer: "coastlines"
                });
            } catch (err) {
                success = false;
            }
            t.ok(!success, "createLayer throws error if provided incomplete layer config");
            
            // bogus layer identifier
            layer = format.createLayer(caps, {
                layer: "foo",
                matrixSet: "BigWorld"
            });
            t.eq(layer, undefined, "createLayer returns undefined given bad layer identifier");
            
            // bogus matrixSet identifier
            layer = format.createLayer(caps, {
                layer: "coastlines",
                matrixSet: "TheWorld"
            });
            t.eq(layer, undefined, "createLayer returns undefined given bad matrixSet identifier");
            
            layer = format.createLayer(caps, {
                layer: "coastlines",
                matrixSet: "BigWorld"
            });
            t.ok(layer instanceof OpenLayers.Layer.WMTS, "correct instance");
            
            t.eq(layer.matrixIds.length, 2, "correct matrixIds length");
            t.eq(layer.name, "Coastlines", "correct layer title");
            t.eq(layer.style, "DarkBlue", "correct style identifier");
        }
        
    </script>
</head>
<body>

<!--
OGC example below taken from
http://schemas.opengis.net/wmts/1.0/examples/wmtsGetCapabilities_response.xml
-->
<div id="ogcsample"><!--
<?xml version="1.0" encoding="UTF-8"?>
<Capabilities xmlns="http://www.opengis.net/wmts/1.0" xmlns:ows="http://www.opengis.net/ows/1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gml="http://www.opengis.net/gml" xsi:schemaLocation="http://www.opengis.net/wmts/1.0 http://schemas.opengis.net/wmts/1.0/wmtsGetCapabilities_response.xsd" version="1.0.0">
    <ows:ServiceIdentification>
        <ows:Title>Web Map Tile Service</ows:Title>
        <ows:Abstract>Service that contrains the map access interface to some TileMatrixSets</ows:Abstract>
        <ows:Keywords>
            <ows:Keyword>tile</ows:Keyword>
            <ows:Keyword>tile matrix set</ows:Keyword>
            <ows:Keyword>map</ows:Keyword>
        </ows:Keywords>
        <ows:ServiceType>OGC WMTS</ows:ServiceType>
        <ows:ServiceTypeVersion>1.0.0</ows:ServiceTypeVersion>
        <ows:Fees>none</ows:Fees>
        <ows:AccessConstraints>none</ows:AccessConstraints>
    </ows:ServiceIdentification>
    <ows:ServiceProvider>
        <ows:ProviderName>MiraMon</ows:ProviderName>
        <ows:ProviderSite xlink:href="http://www.creaf.uab.es/miramon"/>
        <ows:ServiceContact>
            <ows:IndividualName>Joan Maso Pau</ows:IndividualName>
            <ows:PositionName>Senior Software Engineer</ows:PositionName>
            <ows:ContactInfo>
                <ows:Phone>
                    <ows:Voice>+34 93 581 1312</ows:Voice>
                    <ows:Facsimile>+34 93 581 4151</ows:Facsimile>
                </ows:Phone>
                <ows:Address>
                    <ows:DeliveryPoint>Fac Ciencies UAB</ows:DeliveryPoint>
                    <ows:City>Bellaterra</ows:City>
                    <ows:AdministrativeArea>Barcelona</ows:AdministrativeArea>
                    <ows:PostalCode>08193</ows:PostalCode>
                    <ows:Country>Spain</ows:Country>
                    <ows:ElectronicMailAddress>joan.maso@uab.es</ows:ElectronicMailAddress>
                </ows:Address>
            </ows:ContactInfo>
        </ows:ServiceContact>
    </ows:ServiceProvider>
    <ows:OperationsMetadata>
        <ows:Operation name="GetCapabilities">
            <ows:DCP>
                <ows:HTTP>
                    <ows:Get xlink:href="http://www.miramon.uab.es/cgi-bin/MiraMon5_0.cgi?">
                        <ows:Constraint name="GetEncoding">
                            <ows:AllowedValues>
                                <ows:Value>KVP</ows:Value>
                            </ows:AllowedValues>
                        </ows:Constraint>
                    </ows:Get>
                </ows:HTTP>
            </ows:DCP>
        </ows:Operation>
        <ows:Operation name="GetTile">
            <ows:DCP>
                <ows:HTTP>
                    <ows:Get xlink:href="http://www.miramon.uab.es/cgi-bin/MiraMon5_0.cgi?"/>
                </ows:HTTP>
            </ows:DCP>
        </ows:Operation>
        <ows:Operation name="GetFeatureInfo">
            <ows:DCP>
                <ows:HTTP>
                    <ows:Get xlink:href="http://www.miramon.uab.es/cgi-bin/MiraMon5_0.cgi?"/>
                </ows:HTTP>
            </ows:DCP>
        </ows:Operation>
    </ows:OperationsMetadata>
    <Contents>
        <Layer>
            <ows:Title>Coastlines</ows:Title>
            <ows:Abstract>Coastline/shorelines (BA010)</ows:Abstract>
            <ows:WGS84BoundingBox>
                <ows:LowerCorner>-180 -90</ows:LowerCorner>
                <ows:UpperCorner>180 90</ows:UpperCorner>
            </ows:WGS84BoundingBox>
            <ows:Identifier>coastlines</ows:Identifier>
            <ResourceURL format="image/png" resourceType="tile"
                         template="http://www.example.com/wmts/coastlines/{TileMatrix}/{TileRow}/{TileCol}.png" />
            <ResourceURL format="application/gml+xml; version=3.1" resourceType="FeatureInfo"
                         template="http://www.example.com/wmts/coastlines/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}/{J}/{I}.xml" />
            <Style isDefault="true">
                <ows:Title>Dark Blue</ows:Title>
                <ows:Identifier>DarkBlue</ows:Identifier>
                <LegendURL format="image/png" xlink:href="http://www.miramon.uab.es/wmts/Coastlines/coastlines_darkBlue.png"/>
            </Style>
            <Style>
                <ows:Title>Thick And Red</ows:Title>
                <ows:Abstract>Specify this style if you want your maps to have thick red coastlines.
                </ows:Abstract>
                <ows:Identifier>thickAndRed</ows:Identifier>
            </Style>
            <Format>image/png</Format>
            <Format>image/gif</Format>
            <Dimension>
                <ows:Title>Time</ows:Title>
                <ows:Abstract>Monthly datasets</ows:Abstract>
                <ows:Identifier>TIME</ows:Identifier>
                <Value>2007-05</Value>
                <Value>2007-06</Value>
                <Value>2007-07</Value>
                <Default>default</Default>
            </Dimension>
            <TileMatrixSetLink>
                <TileMatrixSet>BigWorld</TileMatrixSet>
            </TileMatrixSetLink>
        </Layer>
        <TileMatrixSet>        
            <ows:Identifier>BigWorld</ows:Identifier>
            <ows:SupportedCRS>urn:ogc:def:crs:OGC:1.3:CRS84</ows:SupportedCRS>
            <TileMatrix>
                <ows:Identifier>1e6</ows:Identifier>
                <ScaleDenominator>1e6</ScaleDenominator>        
                <TopLeftCorner>-180 84</TopLeftCorner>        
                <TileWidth>256</TileWidth>
                <TileHeight>256</TileHeight>        
                <MatrixWidth>60000</MatrixWidth>
                <MatrixHeight>50000</MatrixHeight>
            </TileMatrix>
            <TileMatrix>
                <ows:Identifier>2.5e6</ows:Identifier>
                <ScaleDenominator>2.5e6</ScaleDenominator>
                <TopLeftCorner>-180 84</TopLeftCorner>
                <TileWidth>256</TileWidth>
                <TileHeight>256</TileHeight>
                <MatrixWidth>9000</MatrixWidth>
                <MatrixHeight>7000</MatrixHeight>
            </TileMatrix>
        </TileMatrixSet>
    </Contents>
    <Themes>
        <Theme>
            <ows:Title>Foundation</ows:Title>
            <ows:Abstract>"Digital Chart Of The World" data</ows:Abstract>
            <ows:Identifier>Foundation</ows:Identifier>
            <Theme>
                <ows:Title>Boundaries</ows:Title>
                <ows:Identifier>Boundaries</ows:Identifier>
                <LayerRef>coastlines</LayerRef>
                <LayerRef>politicalBoundaries</LayerRef>
                <LayerRef>depthContours</LayerRef>
            </Theme>
            <Theme>
                <ows:Title>Transportation</ows:Title>
                <ows:Identifier>Transportation</ows:Identifier>
                <LayerRef>roads</LayerRef>
                <LayerRef>railroads</LayerRef>
                <LayerRef>airports</LayerRef>
            </Theme>
        </Theme>
        <Theme>
            <ows:Title>World Geology</ows:Title>
            <ows:Identifier>World Geology</ows:Identifier>
            <LayerRef>worldAgeRockType</LayerRef>
            <LayerRef>worldFaultLines</LayerRef>
            <LayerRef>felsicMagmatic</LayerRef>
            <LayerRef>maficMagmatic</LayerRef>
        </Theme>
    </Themes>
</Capabilities>
--></div>
</body>
</html>
